package com.jason.concurrent.dead;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author  yslai
 * @Date  2022/9/19 15:01
 * @Description 创建一个死锁
 */
public class DeadLock {

    static Object a = new Object();
    static Object b = new Object();

    private Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->{

            synchronized (a)
            {
                System.out.println("a持有锁进行访问b"+Thread.currentThread().getName());
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {


                }
                synchronized (b)
                {
                    System.out.println("a持有锁访问b成功"+Thread.currentThread().getName());
                }
            }

        },"aa").start();

        new Thread(()->{

            synchronized (b)
            {
                System.out.println("b持有锁进行访问a"+Thread.currentThread().getName());
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a)
                {
                    System.out.println("b持有锁访问a成功"+Thread.currentThread().getName());
                }
            }

        },"bb").start();
    }
}
